package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.common.enums.State;
import com.tanhua.common.enums.TopState;
import com.tanhua.common.pojo.Messages;
import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.enums.IdType;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.dubbo.server.service.IdService;
import com.tanhua.dubbo.server.service.TimeLineService;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;


@Service(version = "1.0.0")
@Slf4j
public class QuanZiApiImpl implements QuanZiApi {

    //需要去MongoDB的数据库中查询,注入MongoDB
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdService idService;//维护一套自增长的id
    @Autowired
    private TimeLineService timeLineService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 是谁的key?
     * 答:外键key
     */
    //评论数据存储在Redis中key的前缀
    private static final String COMMENT_REDIS_KEY_PREFIX = "QUANZI_COMMENT_";

    //用户是否点赞的前缀
    private static final String COMMENT_USER_LIEK_REDIS_KEY_PREFIX = "USER_LIKE_";

    //用户是否喜欢的前缀
    private static final String COMMENT_USER_LOVE_REDIS_KEY_PREFIX = "USER_LOVE_";

    @Override
    public PageInfo<Publish> queryPublishList(Long userId, Integer page, Integer pageSize) {
        /**
         *查询好友动态,就是查询好友曾发布的publish,其本质就是利用登录者的id,查询其所有好友的publishId,
         * 再利用publiId查询这个范围下的publish,从而实现查询好友动态
         * 1.当  '我'  登录时,token会携带我的id,
         * 2.利用这个id在表TimeLine中查询到publishId(为什么要去时间线表里查,因为每位用户都有一个时间线表,
         * 时间线表会记录这位登陆者所有好友曾发布的所有publish)
         */
        //先填充pageInfo中的一些给定元素
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        //先利用springframework自带的工具进行分页
        Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("date")));
        //从MongoDB中查询该登陆者时间线表中的全部数据(在该登陆者的时间线表中查出的每一行数据,都与TimeLine匹配,可以看做查出的每一条数据都是一个TimeLine对象)
        Query query = new Query().with(pageable);
        List<TimeLine> timeLines = mongoTemplate.find(query, TimeLine.class, "quanzi_time_line_" + userId);
        //利用"难得糊涂"得到timeLines中所有的publishId;
        List<Object> ids = CollUtil.getFieldValues(timeLines, "publishId");
        //利用得到的ids(即所有的publishId)到表publish查询到好友动态
        /**
         * 说明:在下面的条件构造中为什么不需要指定到哪一张表中去查 原因:在创建Publish对象时,对象就
         * 已经通过"@Document(collection = "quanzi_publish")"注解与表进行了关联
         */
        Query publishQuery = Query.query(Criteria.where("id").in(ids)).with(Sort.by(Sort.Order.desc("created")));
        List<Publish> publishes = mongoTemplate.find(publishQuery, Publish.class);
        //因为最后返回的是PageInfo,不是publis,所以将查询到的动态publishs存入pageInfo
        if (null == publishes){
            return pageInfo;
        }

        for (int i = 0; i < publishes.size(); i++) {
            String state = publishes.get(i).getState();
            if (ObjectUtil.isNotEmpty(state)){
                if (!(ObjectUtil.equals(state,"5"))){
                    publishes.remove(publishes.get(i));
                    i--;
                }
            }
        }
        pageInfo.setRecords(publishes);
        return pageInfo;
    }

    /**
     * 发布动态:主要难点为如何将发布的动态存入三张表
     *
     * @param publish
     * @return
     */
    @Override
    public String savePublish(Publish publish) {
        /**
         * 发布publish,前台必定会传给你一个publish的 内容
         */
        //1.校验publish内是否有必须的内容, 有:写入各个表中, 没有:不写入
        if (!ObjectUtil.isAllNotEmpty(publish.getText(), publish.getUserId())) {
            return null;
        }
        /**
         * 当确定publish必须要写入时,就要明确publish要写入哪些表
         * 1:发布表 2:相册表 3:朋友的时间线表
         */
        //2:存入发布表
        //2.1 设置主键id
        publish.setId(ObjectId.get());
        //2.2 由于pid需要自增,但是在MongoDB的数据库中不能自增,所以需要自己维护一套可以自增的id
        /**
         * 这里利用redis中有自增属性进行设计
         */
        try {
            publish.setPid(idService.createId(IdType.PUBLISH));
            publish.setCreated(System.currentTimeMillis());
            publish.setTopState(1);
            publish.setState("3");
            //2.3 存入发布表
            Publish publish1 = mongoTemplate.save(publish);
            System.out.println(publish1);

            //3:存入发布表
            Album album = new Album();
            album.setId(ObjectId.get());
            album.setCreated(System.currentTimeMillis());
            album.setPublishId(publish.getId());
            this.mongoTemplate.save(album, "quanzi_album_" + publish.getUserId());

            /**
             * 添加到后台消息表
             */
            Messages messages = new Messages();
            messages.setId(ObjectId.get());
            messages.setPublishId(publish.getId());
            messages.setUserId(publish.getUserId());
            messages.setTopState(TopState.NOT_TOPPED);
            messages.setState(State.TO_BE_REVIEWID);
            this.mongoTemplate.save(messages);

            /**
             * 4:存入朋友的时间线表
             *   4.1:存入朋友的时间线表,需要登入者id,publish的id
             */
            timeLineService.saveTimeLine(publish.getUserId(), publish.getId());


        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(publish);
        return publish.getId().toHexString();
    }

    /**
     * 推荐动态
     *
     * @param userId   用户id
     * @param page     当前页数
     * @param pageSize 每一页查询的数据条数
     * @return
     */
    @Override
    public PageInfo<Publish> queryRecommendPublishList(Long userId, Integer page, Integer pageSize) {
        //推荐动态是由pid来查询,pid存储在redis中,它的redisKey="QUANZI_PUBLISH_RECOMMEND_" + userId;
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        String redisKey = "QUANZI_PUBLISH_RECOMMEND_" + userId;
        //这里的data,就是一串由逗号分割的字符串
        String data = redisTemplate.opsForValue().get(redisKey);
        if (StrUtil.isEmpty(data)) {
            return pageInfo;
        }
        //如果data不为空,那么就要将data分割,并且转成Long型数据(因为userId为Long型)
        List<String> strList = StrUtil.split(data, ',');//问: 这里为什么ArrayList不行

        List<Long> pids = new ArrayList<>();
        for (int i = 0; i < strList.size(); i++) {
            pids.add(Long.valueOf(strList.get(i)));
        }
        if (CollUtil.isEmpty(pids)) {
            return pageInfo;
        }


        PageRequest pageRequest = new PageRequest(page - 1, pageSize);
        Query query = Query.query(Criteria.where("pid").in(pids)).with(pageRequest).with(Sort.by(Sort.Order.desc("created")));
        List<Publish> publishes = mongoTemplate.find(query, Publish.class);
        /**
         * 如果动态未审核 不展示给用户
         */

        if (publishes == null) {
            return pageInfo;
        }
        for (int i = 0; i < publishes.size(); i++) {
            String state = publishes.get(i).getState();
            if (ObjectUtil.isNotEmpty(state)){
                if (!(ObjectUtil.equals(state,"5"))){
                    publishes.remove(publishes.get(i));
                    i--;
                }
            }
        }
        pageInfo.setRecords(publishes);
        return pageInfo;
    }

    /**
     * ===================================点赞==================================
     *
     * @param id 动态id
     * @return 根据id查询动态
     * 此处id为String类型,必须转成ObjectId类型进行查询
     */
    @Override
    public Publish queryPublishById(String id) {
        Publish publish = mongoTemplate.findById(new ObjectId(id), Publish.class);
        return publish;
    }

    /**
     * 根据评论id寻找评论
     * @param id
     * @return
     */
    public Comment queryCommentById(String id){
         return mongoTemplate.findById(new ObjectId(id),Comment.class);
    }

    /**
     * 根据视频id寻找视频
     * @param id
     * @return
     */
    public Video queryVideoById(String id){
        return mongoTemplate.findById(new ObjectId(id),Video.class);
    }

    /**
     * @param userId
     * @param publishId
     * @return
     *                      目的:给别人的动态点赞
     * 1.查询该用户是否已经点赞过该动态
     * 1.1 有,直接返回false,说明该动态已经点赞过
     * 1.2 没有
     * 1.2.1将点赞数据存入quanzi_common表
     * 1.2.2 将点赞数据存入redis中
     */
    @Override
    public Boolean likeComment(Long userId, String publishId) {
        Boolean flag = queryUserIsLike(userId, publishId);
        if (flag) {
            //   为true就是有值,有值就是已经点赞过,直接flase
            return false;
        }
        //存入quanzi_comment
        boolean result = saveComment(userId, publishId, CommentType.LIKE,null);
        if (!result) {
            return false;
        }
        //存入redis中
        /**
         * 注意这里的key值,外键redisKey,以及内键的各种key,
         *  redis存储为hash类型:   redisKey:{key:value}
         */
        //外键id
        String redisKey = getRedisKey(publishId);
        //点赞数id
        String hashKey = CommentType.LIKE.toString();
        //点赞标记id
        String userHashKey = getUserLikeHashKey(userId);
        /**
         * 1.修改点赞数:自增1
         * 2.标记该人已点赞
         */
        redisTemplate.opsForHash().increment(redisKey, hashKey, 1);
        redisTemplate.opsForHash().put(redisKey, userHashKey, "1");
        return true;
    }


    @Override
    public Boolean disLikeComment(Long userId, String publishId) {
        Boolean flag = queryUserIsLike(userId, publishId);
        if (!flag) {
            return false;
        }
        //移除quanzi_commend中的数据
        boolean result = removeComment(userId, publishId, CommentType.LIKE);
        if (!result) {
            return false;
        }
        //移除redis中的数据
        //外键id
        String redisKey = getRedisKey(publishId);
        //点赞数id
        String hashKey = CommentType.LIKE.toString();
        //点赞标记id
        String userHashKey = getUserLikeHashKey(userId);
        redisTemplate.opsForHash().increment(redisKey, hashKey, -1);
        redisTemplate.opsForHash().delete(redisKey, userHashKey);
        return true;
    }

    /**
     * 该动态的点赞数
     * 1.先到redis中寻找信息(但redis中有可能遗失信息)
     * 2.再到quan_comment中寻找信息
     * 3.找到信息后还要把信息写入redis中
     *
     * @param publishId
     * @return
     */
    @Override
    public Long queryLikeCount(String publishId) {
        String redisKey = getRedisKey(publishId);
        String hashKey = CommentType.LIKE.toString();
        Object data = redisTemplate.opsForHash().get(redisKey, hashKey);
        if (data != null) {
            return Convert.toLong(data);
        }
        //如果redis中没找到,就要到quanzi_comment中找了

        Query query1 = Query.query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(CommentType.LIKE.getType()));
        Long count = mongoTemplate.count(query1, Comment.class);
        //Long count = queryCommentCount(publishId);
        //这里不用判断count是否有值,因为即使没有值,写到redis中也没有问题
        redisTemplate.opsForHash().put(redisKey, hashKey, String.valueOf(count));
        return count;
    }

    /**
     * 判断登陆者是否点赞
     * 1.先去redis中查询userHashKey是否等于1
     * 1.1 有,该用户已点赞返回ture
     * 1.2 没有,去quanzi_comment中寻找
     * 1.2.1 有,用户已点赞,返回true,并写入redis中
     * 1.2.2 没有,用户 没点赞,返回false
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean queryUserIsLike(Long userId, String publishId) {
        String redisKey = getRedisKey(publishId);
        String userHashKey = getUserLikeHashKey(userId);
        Object data = redisTemplate.opsForHash().get(redisKey, userHashKey);
        if (ObjectUtil.isNotEmpty(data)) {
            return StrUtil.equals(Convert.toStr(data), "1");
        }
        /**
         * 如果在redis中没有找到,就要到quanzi_comment中寻找,
         *  利用三个条件看看到quanzi_comment中能不能找到值
         */
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(CommentType.LIKE.getType()));
        long count = mongoTemplate.count(query, Comment.class);
        if (count == 0) {
            return false;
        }
        //如果count不等于0,写入redis中
        redisTemplate.opsForHash().put(redisKey, userHashKey, "1");
        return true;
    }

    /**
     * 喜欢功能(注:只有推荐动态里才有喜欢功能)
     * <p>
     * 喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean loveComment(Long userId, String publishId) {
        Boolean flag = queryUserIsLove(userId, publishId);
        if (flag) {
            //   为true就是有值,有值就是已经点过喜欢,直接flase
            return false;
        }
        //存入quanzi_comment
        boolean result = saveComment(userId, publishId, CommentType.LOVE,null);
        if (!result) {
            return false;
        }
        //存入redis中
        /**
         * 注意这里的key值,外键redisKey,以及内键的各种key,
         *  redis存储为hash类型:   redisKey:{key:value}
         */
        //外键id
        String redisKey = getRedisKey(publishId);
        //喜欢数id
        String hashKey = CommentType.LOVE.toString();
        //喜欢标记id
        String userHashKey = getUserLoveHashKey(userId);
        /**
         * 1.修改喜欢数:自增1
         * 2.标记该人已喜欢
         */
        redisTemplate.opsForHash().increment(redisKey, hashKey, 1);
        redisTemplate.opsForHash().put(redisKey, userHashKey, "1");
        return true;
    }

    /**
     * 取消喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean disLoveComment(Long userId, String publishId) {
        Boolean flag = queryUserIsLove(userId, publishId);
        if (!flag) {
            return false;
        }
        //移除quanzi_commend中的数据
        boolean result = removeComment(userId, publishId, CommentType.LOVE);
        if (!result) {
            return false;
        }
        //移除redis中的数据
        //外键id
        String redisKey = getRedisKey(publishId);
        //点赞数id
        String hashKey = CommentType.LOVE.toString();
        //点赞标记id
        String userHashKey = getUserLoveHashKey(userId);
        redisTemplate.opsForHash().increment(redisKey, hashKey, -1);
        redisTemplate.opsForHash().delete(redisKey, userHashKey);
        return true;
    }

    /**
     * 喜欢数
     *
     * @param publishId
     * @return
     */
    @Override
    public Long queryLoveCount(String publishId) {
        String redisKey = getRedisKey(publishId);
        String hashKey = CommentType.LOVE.toString();
        Object data = redisTemplate.opsForHash().get(redisKey, hashKey);
        if (data != null) {
            return Convert.toLong(data);
        }
        //如果redis中没找到,就要到quanzi_comment中找了
        Long count = queryCommentCount(publishId);
        //这里不用判断count是否有值,因为即使没有值,写到redis中也没有问题
        redisTemplate.opsForHash().put(redisKey, hashKey, String.valueOf(count));
        return count;
    }

    /**
     * 用户是否点击了喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean queryUserIsLove(Long userId, String publishId) {
        String redisKey = getRedisKey(publishId);
        String userHashKey = getUserLoveHashKey(userId);
        Object data = redisTemplate.opsForHash().get(redisKey, userHashKey);
        if (ObjectUtil.isNotEmpty(data)) {
            return StrUtil.equals(Convert.toStr(data), "1");
        }
        /**
         * 如果在redis中没有找到,就要到quanzi_comment中寻找,
         *  利用三个条件看看到quanzi_comment中能不能找到值
         */
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(publishId))
                .and("commentType").is(CommentType.LOVE.getType()));
        long count = mongoTemplate.count(query, Comment.class);
        if (count == 0) {
            return false;
        }
        //如果count不等于0,写入redis中
        redisTemplate.opsForHash().put(redisKey, userHashKey, "1");
        return true;
    }

    /**
     * 查询评论列表
     * 注:打开评论列表,会单独查询一条动态,且对评论有点赞和取消点赞功能
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> queryCommentList(String publishId, Integer page, Integer pageSize) {
        //先将评论进行分页处理
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));
        Query query = new Query(Criteria
                .where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(CommentType.COMMENT.getType())).with(pageRequest);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        /*if(CollUtil.isEmpty(comments)){
            return pageInfo;
        }*/
        pageInfo.setRecords(comments);
        return pageInfo;
    }

    /**
     * 发表评论
     *
     * @param userId
     * @param publishId
     * @param content
     * @return
     */
    @Override
    public Boolean saveComment(Long userId, String publishId, String content) {
        return saveComment(userId,publishId,CommentType.COMMENT,content);
    }

    /**
     * quan_comment中存评论
     *
     * @param userId
     * @param publishId
     * @return
     */
    public boolean saveComment(Long userId, String publishId, CommentType commentType,String content) {
        try {
            //创造一条新的comment,并存入quanzi_comment
            Comment comment = new Comment();
            comment.setId(ObjectId.get());
            comment.setPublishId(new ObjectId(publishId));
            comment.setUserId(userId);
            comment.setCommentType(commentType.getType());
            comment.setCreated(System.currentTimeMillis());
            comment.setContent(content);
        /*TODO 另有一些信息先不做处理
        comment.setIsParent(false);*/
            //因为不能直接得到发布者的id,所以先利用动态的id查到该动态,再由该动态找到发布者的id
            //Publish publish = mongoTemplate.findById(new ObjectId(publishId), Publish.class);
            Publish publish = queryPublishById(publishId);
            if(ObjectUtil.isNotEmpty(publish)){
                comment.setPublishUserId(publish.getUserId());
            }else {
                //注:当这个publishId查不到动态时,说明此时这个不是动态iD,而现在它是评论的id;
                Comment myComment=queryCommentById(publishId);
                if(ObjectUtil.isNotEmpty(myComment)){
                    comment.setPublishUserId(myComment.getUserId());
                }else {
                    //注:当这个publishId查不到动态也查不到评论时,说明此时这个不是动态iD,也不是评论id,而现在它是视频的id;
                    Video video = queryVideoById(publishId);
                    if(ObjectUtil.isNotEmpty(video)){
                        comment.setPublishUserId(video.getUserId());
                    }else {
                        return false;
                    }
                }

            }
            mongoTemplate.save(comment);
            return true;
        } catch (Exception e) {
            log.error("quan_comment存储失败", e);
        }
        return false;
    }

    /**
     * quan_comment中删评论
     *
     * @param userId
     * @param publishId
     * @return
     */
    public boolean removeComment(Long userId, String publishId, CommentType commentType) {
        //使用混合条件找到唯一评论comment
        try {
            Query query = Query.query(Criteria.where("userId").is(userId).and("publishId").is(new ObjectId(publishId)).and("commentType").is(commentType.getType()));
            boolean result = mongoTemplate.remove(query, Comment.class).getDeletedCount() > 0;
            return result;
        } catch (Exception e) {
            log.error("抱歉,删评错误", e);
        }
        return false;
    }



    /**
     * 得到redisKey
     *
     * @param publishId
     * @return
     */
    public String getRedisKey(String publishId) {
        return COMMENT_REDIS_KEY_PREFIX + publishId;
    }

    /**
     * 得到userHashKey
     *
     * @param userId
     * @return
     */
    public String getUserLikeHashKey(Long userId) {
        return COMMENT_USER_LIEK_REDIS_KEY_PREFIX + userId;
    }

    /**
     * 得到userHashKey
     *
     * @param userId
     * @return
     */
    public String getUserLoveHashKey(Long userId) {
        return COMMENT_USER_LOVE_REDIS_KEY_PREFIX + userId;
    }

    /**
     * quanzi_comment获取点赞数
     */
    public Long queryCommentCount(String publishId) {
        long count = 0;
        try {
                Query query1 = Query.query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(CommentType.COMMENT.getType()));
                count = mongoTemplate.count(query1, Comment.class);

        } catch (Exception e) {
            log.error("抱歉,查询错误", e);
        }
        return count;
    }

    /**
     * 查询对我的点赞消息列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> queryLikeCommentListByUser(Long userId, Integer page, Integer pageSize) {
        return queryCommentListByUser(userId,CommentType.LIKE,page,pageSize);
    }

    /**
     * 查询对我的喜欢消息列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> queryLoveCommentListByUser(Long userId, Integer page, Integer pageSize) {
        return queryCommentListByUser(userId,CommentType.LOVE,page,pageSize);
    }

    /**
     * 查询对我的评论消息列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> queryCommentListByUser(Long userId, Integer page, Integer pageSize) {
        return queryCommentListByUser(userId,CommentType.COMMENT,page,pageSize);
    }

    /**
     * 在MongoDB中查询对我发布东西的点赞,评论,喜欢,
     * @param userId
     * @param commentType
     * @param page
     * @param pageSize
     * @return
     */
    public PageInfo<Comment> queryCommentListByUser(Long userId, CommentType commentType,Integer page, Integer pageSize){

        //1.分页
        PageRequest  pageRequest = PageRequest.of(page-1,pageSize,Sort.by(Sort.Order.desc("created")));
        //这里的id是发布东西的那个人的id
        Query query = Query.query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType.getType())).with(pageRequest);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(comments);
        return pageInfo;
    }

    /**
     * 查询个人相册
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Publish> queryAlbumList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Publish> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        PageRequest pageRequest = PageRequest.of(page-1,pageSize,Sort.by(Sort.Order.desc("created")));
        /**
         * 这种方式是对字段内的数据加上条件
         * Query query = Query.query(Criteria.where(""));
         */

        Query query = new Query().with(pageRequest);
        //参数不同,这里是指查询到是哪一张数据表
        List<Album> albums = mongoTemplate.find(query, Album.class, "quanzi_album_" + userId);
        if(ObjectUtil.isEmpty(albums)){
            return pageInfo;
        }
        //拿到个人相册中所有的publishId
        List<Object> publishIds = CollUtil.getFieldValues(albums, "publishId");
        Query pubishQuery =  Query.query(Criteria.where("id").in(publishIds)).with(Sort.by(Sort.Order.desc("created")));
        List<Publish> publishes = mongoTemplate.find(pubishQuery, Publish.class);
        if(ObjectUtil.isEmpty(publishes)){
            return pageInfo;
        }
        pageInfo.setRecords(publishes);
        return pageInfo;
    }

    @Override
    public void quellAll() {
        List<Publish> publishList = mongoTemplate.findAll(Publish.class);
        for (Publish publish : publishList) {
            Messages messages = new Messages();
            messages.setId(ObjectId.get());
            messages.setPublishId(publish.getId());
            messages.setUserId(publish.getUserId());
            messages.setTopState(TopState.NOT_TOPPED);
            messages.setState(State.TO_BE_REVIEWID);
            mongoTemplate.save(messages);
        }
    }
}
